package com.example.xm.service.impl;

import com.example.xm.entity.DingCai;
import com.example.xm.entity.Fenlei;
import com.example.xm.entity.Gedan;
import com.example.xm.entity.Yinyue;
import com.example.xm.exception.SerceiceException;
import com.example.xm.mapper.DingcaiMapper;
import com.example.xm.mapper.FenleiMapper;
import com.example.xm.mapper.YinyueMapper;
import com.example.xm.service.IDingcaiService;
import com.example.xm.service.IFenleiService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DingcaiService implements IDingcaiService {
    @Autowired
    DingcaiMapper dingcaiMapper;
    @Autowired
    YinyueMapper yinyueMapper;
    @Override
    public void shoucang(Integer uid, Integer vid,Integer type) {
        List<DingCai> list= dingcaiMapper.selectList(uid,vid,type);
        DingCai dingCai = new DingCai();
        dingCai.setUid(uid);
        dingCai.setVid(vid);
        dingCai.setType(type);
       if(list.size()>0){
           if(type==1){
               yinyueMapper.updateDianzan(vid,-1);
           }
           dingcaiMapper.del(dingCai);
       }else{
           if(type==1){
               yinyueMapper.updateDianzan(vid,1);
           Yinyue yinyue = yinyueMapper.selectByPrimaryKey(vid);
           dingCai.setCid(yinyue.getCid());
           }

           dingcaiMapper.add(dingCai);
       }
    }

    @Override
    public List<Yinyue> list(Integer uid, Integer type) {
        return  dingcaiMapper.list(uid,type);
    }

    @Override
    public void addGedan(Integer uid, Integer vid, Integer gid) {
      List<Gedan> list=  dingcaiMapper.select(uid,vid,gid);
      if(list.size()>0){
          throw new SerceiceException(400,"已在该歌单");
      }
        dingcaiMapper.addGedan(uid,vid,gid);
    }
}
